//
//  BGZNXBasicalController.m
//  bigMall
//
//  Created by ios on 16/8/30.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGZNXBasicalController.h"
#import "BGZNXCell.h"
#import "BGDateResult.h"
#import "BGOwnMessageListResultModel.h"
#import "BGZNXDetailViewController.h"

@interface BGZNXBasicalController () <UITableViewDelegate,UITableViewDataSource> {
    
    NSMutableArray *_dataArr;
    UITableView *_tableView;
    int index;
    UIImageView *imageView;
    UILabel *lable;
    
}

@end

@implementation BGZNXBasicalController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self createUi];
    [self initDat];
    
}
- (void)initDat {
    
    _dataArr = [NSMutableArray array];
    
}
- (void)createUi {
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, BGWidth, BGHight - 106) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.view addSubview:_tableView];
    
    //刷新的加载
    _tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(index)];
    
    [_tableView.header beginRefreshing];
    //判断数据源有无
    
    imageView = [[UIImageView alloc] initWithFrame:CGRectMake((BGWidth - 70 ) / 2 + 10, (_tableView.height - 109 - 160) / 3, 70, 90)];
    imageView.image = [UIImage imageNamed:@"抱歉~暂无数据"];
    
    lable = [[UILabel alloc] initWithFrame:CGRectMake((BGWidth - 120 ) / 2 + 10, imageView.bottom + 21, 120, 16)];
    lable.text = @"抱歉~暂无数据";
    lable.font = [UIFont systemFontOfSize:16];
    [_tableView addSubview:lable];
    
    [_tableView addSubview:imageView];
    lable.hidden = YES;
    imageView.hidden = YES;
    
}
- (void)addIndex {
    
    index++;
    [self loadInter];
    
}
- (void)index {
    
    index = 1;
    [_dataArr removeAllObjects];
    [self loadInter];
    
}
- (void)loadInter {
    
    NSLog(@"type:%@",self.type);
    [BGDateResult ownMessageListWithThisPage:[NSString stringWithFormat:@"%d",index] pageNum:@"10" type:self.type success:^(NSObject *result) {
        
        BGOwnMessageListResultModel *model = [[BGOwnMessageListResultModel alloc] init];
        model = (BGOwnMessageListResultModel *)result;
//        NSLog(@"aaaa%@",model.status);
//        NSLog(@"bbbb%@",model.list);
        NSArray *listArr = [NSArray arrayWithArray:model.list];
        
        for (BGOwnMessageListValueModel *message in listArr) {
            [_dataArr addObject:message];
        }
        
//        _dataArr = [NSMutableArray arrayWithArray:listArr];
        
        if (_dataArr.count == 0 && index == 1) {
            
            [_tableView.header endRefreshing];
            [_tableView.footer endRefreshingWithNoMoreData];
            
            //判断数据源有无
            
            lable.hidden = NO;
            imageView.hidden = NO;
        }

        if ([model.allPage intValue] > index) {
            _tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(addIndex)];
        }
        if (listArr.count == 0) {
            [_tableView.footer endRefreshingWithNoMoreData];
            return;
        }
        lable.hidden = YES;
        imageView.hidden = YES;
        [_tableView.footer endRefreshing];
        [_tableView.header endRefreshing];
        [_tableView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
    
}
#pragma mark - delegate
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (_dataArr.count != 0) {
        
        return _dataArr.count;
        
    } else {
        return 0;
    }
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.1;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.1;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 90;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView registerClass:[BGZNXCell class] forCellReuseIdentifier:@"celId"];
    BGZNXCell *cel = [tableView dequeueReusableCellWithIdentifier:@"celId" forIndexPath:indexPath];
    BGOwnMessageListValueModel *model = [[BGOwnMessageListValueModel alloc] init];
    if (_dataArr.count != 0) {
        
        model = _dataArr[indexPath.row];
        cel.timeStr = model.create_time;
        cel.wuzhongStr = model.title;
        cel.remarkStr = model.content;
    }
   
    return cel;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    BGZNXDetailViewController *a = [[BGZNXDetailViewController alloc] init];
    BGOwnMessageListValueModel *model = [[BGOwnMessageListValueModel alloc] init];
    if (_dataArr.count != 0) {
        
        model = _dataArr[indexPath.row];
        a.add_time = model.create_time;
        a.title1 = model.title;
        a.cont = model.content;
        a.fromname = model.fromName;
        
    }

    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:a animated:YES];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return YES;
    
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return UITableViewCellEditingStyleDelete;
    
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //删除对应数据的cell
        BGOwnMessageListValueModel *model = [[BGOwnMessageListValueModel alloc] init];
        if (_dataArr.count != 0) {
            
            model = _dataArr[indexPath.row];
            
        }
        
        NSLog(@"%@",model.idStr);
        
        [self delate:model.idStr];
        [_dataArr removeObjectAtIndex:indexPath.row];  //删除数组里的数据
        
        [tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];
        
    }
    
}
- (void)delate:(NSString *)idStr {
    
    [BGDateResult deleteMessageWithIdStr:idStr success:^(NSObject *result) {
        
        BGsendMessageModel *model = (BGsendMessageModel *)result;
        if ([model.status isEqualToString:@"1"]) {
            [self showMessage:model.msg];
        } else {
            [self showMessage:model.msg];
        }
        
    } failure:^(NSError *error) {
        
    }];
    
    
}
- (void)showMessage:(NSString *)message {
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    
    [alertController addAction:okAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
    
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return @"删除";
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//- (void)viewWillDisappear:(BOOL)animated {
//    self.tabBarController.tabBar.hidden = YES;
//}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
